home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libspool / submit.c.z / submit.c
C/C++ Source or Header  |  1996-05-06  |  5KB  |  195 lines

  1. /**************************************************************************
  2.  *                                      *
  3.  *           Copyright (c)    1991 Silicon Graphics, Inc.          *
  4.  *            All Rights Reserved                    *
  5.  *                                      *
  6.  *       THIS    IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI          *
  7.  *                                      *
  8.  * The copyright notice above does not evidence any actual of intended      *
  9.  * publication of such source code, and is an unpublished work by Silicon *
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is *
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or      *
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly *
  13.  * prohibited.                                  *
  14.  *                                      *
  15.  * RESTRICTED RIGHTS LEGEND:                          *
  16.  *                                      *
  17.  * Use, duplication or disclosure by the Government is subject to      *
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in      *
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,      *
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR      *
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of  *
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.      *
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311              *
  24.  **************************************************************************
  25.  *
  26.  * File: submit.c
  27.  *
  28.  * Description: Submits a job for printing on the specified spooling system.
  29.  *
  30.  **************************************************************************/
  31.  
  32.  
  33. #ident "$Revision: 1.1 $"
  34.  
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <time.h>
  40. #ifdef sgi
  41. #include <getopt.h>
  42. #endif
  43. #include "spool.h"
  44.  
  45.  
  46. char *filename;
  47. char *title;
  48. char *printer;
  49. int num_copies = 1;        /* Print one copy by default */
  50. int copy = 0;            /* Spooler should link to file to be printed */
  51. int mail = 0;            /* Do not send mail by default */
  52. char *options;            /* Spooling system specific options */
  53. int spooler = SL_SPOOLER_NONE;    /* Indicate no spooling system set */
  54.  
  55.  
  56. extern char *optarg;
  57. extern int optind;
  58.  
  59.  
  60. void usage(char*);
  61.  
  62.  
  63. main(int argc, char *argv[])
  64. {
  65.     int ch;
  66.     int errflag = 0, stdinflag = 0;
  67.     register int i;
  68.     SLPrintJob *job;        /* Refer to SLSubmitJob(3) man page */
  69.  
  70.     /*
  71.      * Process command line args
  72.      */
  73.     while ((ch = getopt(argc, argv, "n:d:cmt:p:s:")) != -1) {
  74.     switch(ch) {
  75.         case 'n':        /* Number of copies */
  76.         num_copies = atoi(optarg);
  77.         break;
  78.         case 'd':        /* Destination printer */
  79.         printer = optarg;
  80.         break;
  81.         case 'c':        /* Copy or link to spool dir */
  82.         copy = 1;
  83.         break;
  84.         case 'm':        /* Send mail on completion */
  85.         mail = 1;
  86.         break;
  87.         case 't':        /* Banner title */
  88.         title = optarg;
  89.         break;
  90.         case 'p':        /* Spooler/Printer specific options */
  91.                 /*    To send printer specific options */
  92.                 /*    type '-p "-ozoom 0.5"' */
  93.         options = optarg;
  94.         break;
  95.         case 's':        /* Spooler to use */
  96.         if (!strcmp(optarg, "bsd"))
  97.             spooler = SL_SPOOLER_BSD;
  98.         else if (!strcmp(optarg, "sysv"))
  99.             spooler = SL_SPOOLER_SYSV;
  100.         else
  101.             errflag++;
  102.         break;
  103.         case '?':
  104.         default:
  105.         errflag++;
  106.         break;
  107.     }
  108.     }
  109.  
  110.     /*
  111.      * Get the filename(s). If no filename has been specified, read
  112.      * stdin
  113.      */
  114.     if (argc == optind)
  115.     stdinflag++;
  116.     else {
  117.     for (i = optind; i < argc; i++) {
  118.         int len = strlen(argv[i]);
  119.         if (filename)
  120.         filename = (char*)realloc(filename, strlen(filename)+len+5);
  121.         else {
  122.         filename = (char*)malloc(len + 5);
  123.         *filename = '\0';
  124.         }
  125.         (void)strcat(filename, argv[i]);
  126.         (void)strcat(filename, " ");
  127.     }
  128.     }
  129.  
  130.     /*
  131.      * If error print usage and exit
  132.      */
  133.     if (errflag) {
  134.     usage(argv[0]);
  135.     exit(1);
  136.     }
  137.  
  138.     /*
  139.      * If a spooling system has been specified set it
  140.      */
  141.     if (spooler != SL_SPOOLER_NONE) {
  142.     if (SLSetSpooler(spooler) < 0) {
  143.         SLPerror(argv[0]);
  144.         exit(1);
  145.     }
  146.     }
  147.  
  148.     /*
  149.      * Submit the job. Note that we do not need to get the default printer
  150.      * name if no printer name was specified on the command-line. This is
  151.      * because SLSubmitJob will determine the default printer automatically
  152.      * if the printer parameter is NULL.
  153.      */
  154.     if (stdinflag)    /* Print from standard input */
  155.         job = SLSubmitJobFd(fileno(stdin), printer, num_copies, copy, mail,
  156.                             title, options);
  157.     else        /* Print from file(s) */
  158.         job = SLSubmitJob(filename, printer, num_copies, copy, mail,
  159.                             title, options);
  160.     if (job == NULL) {
  161.     int j, status, nout;
  162.     char **out_buf;
  163.     SLPerror(argv[0]);
  164.     if ((status = SLGetSpoolerError(&out_buf, &nout)) != 0) {
  165.         (void)fprintf(stderr, "Spooler exit status: %d\n", status);
  166.         (void)fprintf(stderr, "Spooler error message(s):\n");
  167.         for (j = 0; j < nout; j++)
  168.             (void)fprintf(stderr, "\t%s\n", out_buf[j]);
  169.     }
  170.     exit(1);
  171.     }
  172.  
  173.     /*
  174.      * Print job info
  175.      */
  176.     (void)printf("Job submitted\n\n");
  177.     (void)printf("Spooler: \t%s\n",
  178.             (job->spooler == SL_SPOOLER_BSD) ? "BSD":"Sys V");
  179.     (void)printf("Printer: \t%s\n", job->printer);
  180.     (void)printf("File(s): \t%s\n", job->filename);
  181.     (void)printf("User: \t\t%s\n", job->username);
  182.     (void)printf("Job ID: \t%s\n", (job->job_id) ? job->job_id: "Unknown");
  183.     (void)printf("Time: \t\t%s\n", ctime(&job->time_stamp));
  184.  
  185.     return 0;
  186. }
  187.  
  188.  
  189. void usage(char *progname)
  190. {
  191.     (void)fprintf(stderr, "%s [-n #] [-d printer] [-c] [-t title] ", progname);
  192.     (void)fprintf(stderr,
  193.         "[-p spooler options] [-s bsd | sysv] [filename(s)]\n");
  194. }
  195.